﻿using System;
using System.Collections.Generic;
using System.Text;
using SalonPay.IBLL;
using SalonPay.Entity;
using System.Configuration;

namespace SalonPay.ChargeService
{
    public class FailedChargeService
    {
        public FailedChargeService()
        {
            serviceController = true;
        }

        /// <summary>
        /// 休眠时间间隔
        /// </summary>
        private readonly int sleepTimeSpan = Convert.ToInt32(ConfigurationManager.AppSettings["sleepTime"]);
        /// <summary>
        /// 循环控制器
        /// </summary>
        private bool serviceController;

        /// <summary>
        /// 开始轮循对充值失败的订单进行充值
        /// </summary>
        public void Start()
        {
            while (serviceController)
            {
                try
                {
                    //开始充值
                    StartCharge();
                }
                catch (Exception exc)
                {
                    //异常，记录日志
                }
                System.Threading.Thread.Sleep(sleepTimeSpan);
            }
        }

        /// <summary>
        /// 停止轮循
        /// </summary>
        public void Stop()
        {
            //停止充值
            serviceController = false;
            System.Threading.Thread.Sleep(500);
        }

        /// <summary>
        /// 充值
        /// </summary>
        private void StartCharge()
        {
            try
            {
                IOrder orderOpt = RemotingHelper.OrderService();
                tb_Order orderInfo = orderOpt.GetChargedFailOrder(); //status = 4;

                if (orderInfo != null)
                {
                    string charge_msg = orderOpt.Charge(orderInfo);//进行充值

                    tb_ChargeLog chargelog = new tb_ChargeLog();
                    chargelog.LOrderId = orderInfo.OrderID;
                    chargelog.LOrderAmount = orderInfo.OAmount;

                    chargelog.LUserName = orderInfo.OUserName;
                    chargelog.LChargeType = "SERVICE";
                    chargelog.LChargeResult = charge_msg;

                    if (charge_msg.Split(new char[1] { '|' }, StringSplitOptions.None)[0] == "true")//充值成功
                    {
                        //更新订单状态为已充值(3)
                        orderOpt.UpdateOrderStatus_Charged(orderInfo.OrderID);

                        chargelog.LChargeStatus = 3; //充值成功
                    }
                    else//充值失败
                    {
                        //更新订单状态为充值失败(4)                                
                        orderOpt.UpdateOrderStatus_ChargeFailed(orderInfo.OrderID);

                        chargelog.LChargeStatus = 4; //充值失败
                    }

                    orderOpt.AddChargeLog(chargelog);
                }
                
            }
            catch (Exception exc)
            {
                //记录异常日志                
            }
        }
    }
}